Storage Classes in C

Storage classes in C are used to determine the lifetime, visibility, memory location, and initial value of a variable. There are four types of storage classes in C
Storage Classes Storage Place Default Value Scope Lifetime
auto RAM Garbage Value Local Within function
extern RAM Zero Global Till the end of the main program Maybe declared anywhere in the program
static RAM Zero Local Till the end of the main program, Retains value between multiple functions call
register Register Garbage Value Local Within the function

Automatic

Example 1

    
        #include   
            int main()  
            {  
            int a; //auto  
            char b;  
            float c;   
            printf("%d %c %f",a,b,c); // printing initial default value of automatic variables a, b, and c.   
            return 0;  
            }   
    
            

Output

garbage garbage garbage

Example 2


    #include   
        int main()  
        {  
        
        int a = 10,i;   
        printf("%d ",++a);  
        {  
        int a = 20;   
        for (i=0;i<3;i++)  
        {  
        printf("%d ",a); // 20 will be printed 3 times since it is the local value of a  
        }  
        }  
        printf("%d ",a); // 11 will be printed since the scope of a = 20 is ended.   
        }    

        

Output

11 20 20 20 11

Static

Example 1


    #include  
        static char c;  
        static int i;  
        static float f;   
        static char s[100];  
        void main ()  
        {  
        printf("%d %d %f %s",c,i,f); // the initial default value of c, i, and f will be printed.   
        }  

        

Output

Pause Next Mute Current Time 3:55 / Duration 18:10 Fullscreen 0 0 0.000000 (null)

Example 2


    #include  
        void sum()  
        {  
        
        static int a = 10;  
        static int b = 24;   
        printf("%d %d n",a,b);  
        a++;   
        b++;  
        }  
        void main()  
        {  
        int i;  
        for(i = 0; i< 3; i++)  
        {  
        sum(); // The static variables holds their value between multiple function calls.    
        }  
        }   

        

Output

10 24 11 25 12 26

Register

Example 1


    #include   
        int main()  
        {  
        register int a; // variable a is allocated memory in the CPU register. The initial default value of a is 0.   
        printf("%d",a);  
        }  

        

Output

0

Example 2


    #include   
        int main()  
        {  
        register int a = 0;   
        printf("%u",&a); // This will give a compile time error since we can not access the address of a register variable.   
        }  

        

Output

main.c:5:5: error: address of register variable ?a? requested printf("%u",&a); ^~~~~~

External

Example 1


    #include   
        int main()  
        {  
        extern int a;   
        printf("%d",a);  
        }  

        

Output

main.c:(.text+0x6): undefined reference to `a' collect2: error: ld returned 1 exit status

Example 2


    #include   
        int a;   
        int main()  
        {  
        extern int a; // variable a is defined globally, the memory will not be allocated to a  
        printf("%d",a);  
        }  

        

Output

0

Example 3


    #include   
        int a;   
        int main()  
        {  
        extern int a = 0; // this will show a compiler error since we can not use extern and initializer at same time   
        printf("%d",a);  
        }  

        

Output

compile time error main.c: In function ?main?: main.c:5:16: error: ?a? has both ?extern? and initializer extern int a = 0;

Example 4


    #include   
        int main()  
        {  
        extern int a; // Compiler will search here for a variable a defined and initialized somewhere in the pogram or not.   
        printf("%d",a);  
        }  
        int a = 20;  

        

Output

20

Example 5


    extern int a;  
    int a = 10;   
    #include   
    int main()  
    {  
    printf("%d",a);  
    }  
    int a = 20; // compiler will show an error at this line  

        

Output

compile time error